package com.cqhfan.auth.config;

import cn.hutool.core.collection.CollUtil;
import com.cqhfan.auth.interceptors.LoginUserAuthInterceptor;
import com.cqhfan.auth.interceptors.LoginUserInterceptor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private AuthProperties authProperties;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册登录拦截器
        registry.addInterceptor(new LoginUserInterceptor(stringRedisTemplate)).order(0);
        if (!authProperties.getEnabled()) {
            // 无需拦截
            return;
        }
        InterceptorRegistration registration = registry.addInterceptor(new LoginUserAuthInterceptor()).order(1);

        // 添加不拦截路径
        List<String> excludePaths = authProperties.getExcludePaths();
        if (CollUtil.isNotEmpty(excludePaths)) {
            registration.excludePathPatterns(excludePaths);
        }
    }

}
